package com.lry.netty01.server.utils;

import com.lry.netty01.server.entity.Packet;
import gnu.io.CommPortIdentifier;
import io.netty.channel.Channel;

import java.util.*;


public class ChannelUtil {

    public static Queue<Packet> pendingQueue = new LinkedList<>();

    public static boolean checkChannel(Channel ctx){
        if(null==ctx||!ctx.isOpen()||!ctx.isActive()){
            return false;
        }
        return true;
    }


    public static void clearCache(short devId,Channel ctx){
        if(null!=ctx){
            ctx.closeFuture().cancel(true);
            ctx.close();
        }
    }

    public static List<String> findComPort() {
        Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers();
        List<String> portNameList = new ArrayList<>();
        while (portList.hasMoreElements()) {
            String portName = portList.nextElement().getName();//名称如 COM1、COM2....
            portNameList.add(portName);
        }
        return portNameList;
    }
}
